<?php
error_reporting(E_ERROR|E_PARSE);
if (!class_exists("go_sql")){
// include("../conf/wsdl.le.je.ll");
	include("/var/www/html/open/conf/wsdl.le.je.ll");
  $le=new go_sql();
  $le->connect();
}
// include("../fun_pft.php");
include("/var/www/html/open/fun_pft.php");
//include("price.fun.php");

//查询语言
/*
// $str="select id,vtype,lang,qx from dataex where account=\"100019\"  and vtype=0 limit 1";
// $str="select * from dataex where account=\"100016\"  and vtype=0 limit 1";
// $GLOBALS['le']->query($str);
// $row = $GLOBALS['le']->fetch_assoc();
// print_r($row );
*/
//更新语言
/*
// $str = "update dataex set lang=1 where account=62 limit 1";
// $GLOBALS['le']->query($str);
*/
exit;
class PFT_server {
	public function get_Dynamic_Price_Merge($pid, $date='', $mode=0, $sdate='', $edate='', $ptype=0, $get_storage=0) {
		if (is_numeric($pid)===false) return "error1";
		//取单个价格
		$onday=date('w',strtotime($date));
		if ($mode==1 && $date){
			//特殊日价格
			$q_s_price=($ptype==0)?'s_price':'l_price as s_price';
			$str="select $q_s_price,storage from uu_product_price where start_date<='$date' and end_date>='$date' and pid=$pid and ptype=1 and string_bj(weekdays,$onday)=1 order by id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$s_price=$GLOBALS['le']->f('s_price');
			$storage=$GLOBALS['le']->f('storage');
			echo  $str;exit;
			if (is_numeric($s_price)) {
				if ($get_storage==1) return "$s_price,$storage";
				else return $s_price;
			}
			$q_s_price=($ptype==0)?'n_price':'l_price as n_price';
			$str="select $q_s_price,storage from uu_product_price where start_date<='$date' and end_date>='$date' and pid=$pid and ptype=0 and string_bj(weekdays,$onday)=1 order by id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$n_price=$GLOBALS['le']->f('n_price');
			$storage=$GLOBALS['le']->f('storage');
			if (is_numeric($n_price)) {
				if ($get_storage==1) return "$n_price,$storage";
				else return $n_price;
			}

			return -1;
		}elseif($mode==2){
			//单个有效最低价
			$q_s_price=($ptype==0)?'s_price':'l_price';
			$str="select min($q_s_price) as mp from uu_product_price where date_add(end_date,interval 1 day)>now() and ptype=1 and pid=$pid and $q_s_price>=0";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$mp=$GLOBALS['le']->f('mp');

			$q_s_price=($ptype==0)?'n_price':'l_price';
			$str="select min($q_s_price) as np from uu_product_price where date_add(end_date,interval 1 day)>now() and ptype=0 and pid=$pid and $q_s_price>=0";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$np=$GLOBALS['le']->f('np');

			if ($mp===NULL && $np!==NULL) $lp=$np;
			elseif ($np===NULL && $mp!==NULL) $lp=$mp;
			elseif ($mp!==NULL && $np!==NULL) $lp=($mp>$np)?$np:$mp;
			//if (is_numeric($lp) && $lp>=0) return $lp;
			else $lp=-1;
			return $lp;
		}else{
			$str="select id,pid,ptype,n_price,s_price,l_price,start_date,end_date,memo,weekdays,storage from uu_product_price where pid=$pid";
			if ($sdate!=='' && $edate!=='') $str.=" and greatest(start_date,'$sdate')<=least(end_date,'$edate')";
			$str.=" order by id desc";
			//$a5[]=array("fprice"=>$fprice);
        	$GLOBALS['le']->query($str);
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}
			return toXML($this->lang,$a5);
		}
    }
	
	public function Dynamic_Storage_Check($tid, $dtype, $times=NULL, $nowdate=NULL, $XMLC=0) {
		//if ($this->authrize($ac, $pw)){
		if (!$tid) return errXML($this->err11);

		//每日库存
		if ($dtype==1){
			$ar1=explode(',',$times);
			$axml=array();
			//每日实时库存
			foreach ($ar1 as $k=>$v){
				$str="select sum(tnum) as cc from uu_ss_order s left join uu_order_fx_details fx on s.ordernum=fx.orderid where s.begintime='$v' and s.tid='$tid' and fx.pay_status<>2 and (s.status=0 or s.status=1 or s.status=6)";
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$cc=$GLOBALS['le']->f('cc');

				$axml[]=$v."|".$cc;
			}
			$xstring=implode(',',$axml);
		}elseif ($dtype==2){
			//$axml=array();
			//实时总库存
			$str="select sum(tnum) as cc from uu_ss_order s left join uu_order_fx_details fx on s.ordernum=fx.orderid where s.begintime>='$nowdate' and s.tid='$tid' and fx.pay_status<>2 and (s.status=0 or s.status=1 or s.status=6)";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$cc=$GLOBALS['le']->f('cc');
			$xstring=$cc;
		}


		if ($XMLC==0){
			$a5=array();
			$a5[]=array("Xstr"=>$xstring);
			return toXML($this->lang,$a5);
		}else{
			return $xstring;
		}



		//}else{
		//return errXML($this->err1);
		//}

    }
}

$pft = new PFT_server();
$ddprice = $pft->get_Dynamic_Price_Merge('25079','2015-11-24',1,"","",0,1);
echo $ddprice;exit;
$a_ddprice=explode(',',$ddprice);
$ddprice=$a_ddprice[0];
$ddstorage=$a_ddprice[1];
echo $ddstorage;exit;
if ($ddstorage != -1) {
	//获取实时库存
	$dcstorage=$pft->Dynamic_Storage_Check('27898',1,"2015-11-23","",1);
	$a_dcstorage=explode('|',$dcstorage);
	$d_cstorage=$a_dcstorage[1];
	$d_cstorage=(int)$d_cstorage;
	echo $d_cstorage;
	echo "<br/>";
	echo $ddstorage;
	echo "<br/>";
	if ($d_cstorage>$ddstorage) echo 111;
	else echo 222;
}
		
		
// $dcstorage = $pft->Dynamic_Storage_Check('27898',2,'',"2015-11-23",0);
// $dcstorage = $pft->Dynamic_Storage_Check('27898',1,"2015-11-23","",1);
// $a_dcstorage=explode('|',$dcstorage);
// $d_cstorage=$a_dcstorage[1];
// $d_cstorage=(int)$d_cstorage+$tnum;
// print_r($d_cstorage);
// echo "<br/>";
// echo $ddstorage;
?>// print_r($d_cstorage);
// echo "<br/>";
// echo $ddstorage;
?>